package com.projectsquirrel;


import java.util.ArrayList;
import java.util.regex.Pattern;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class savedStories extends ListActivity {
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savedstories);
        String[] fileNames = fileList();
        ArrayList<String> fileNamesFiltered = new ArrayList<String>();
        for(int i=0; i<fileNames.length; i++)
        {
        	if (Pattern.matches(".*_Story", fileNames[i]))
        	{
        		fileNamesFiltered.add(fileNames[i].substring(0, fileNames[i].length()-6));
        	}
        }
        fileNames = fileNamesFiltered.toArray(new String[fileNamesFiltered.size()]);
        setListAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, fileNames));      
    }
	@Override
	public void onResume() {
		super.onResume();
        String[] fileNames = fileList();
        ArrayList<String> fileNamesFiltered = new ArrayList<String>();
        for(int i=0; i<fileNames.length; i++)
        {
        	if (Pattern.matches(".*_Story", fileNames[i]))
        	{
        		fileNamesFiltered.add(fileNames[i].substring(0, fileNames[i].length()-6));
        	}
        }
        fileNames = fileNamesFiltered.toArray(new String[fileNamesFiltered.size()]);
        setListAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, fileNames));      
    }
	@Override
	public void onRestart() {
		super.onRestart();
        String[] fileNames = fileList();
        ArrayList<String> fileNamesFiltered = new ArrayList<String>();
        for(int i=0; i<fileNames.length; i++)
        {
        	if (Pattern.matches(".*_Story", fileNames[i]))
        	{
        		fileNamesFiltered.add(fileNames[i].substring(0, fileNames[i].length()-6));
        	}
        }
        fileNames = fileNamesFiltered.toArray(new String[fileNamesFiltered.size()]);
        setListAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, fileNames));      
    }
	@Override
	public void onPause() {
		super.onPause();
        //final ListView list = (ListView) findViewById(android.R.layout.simple_list_item_1);
        String[] fileNames = fileList();
        ArrayList<String> fileNamesFiltered = new ArrayList<String>();
        for(int i=0; i<fileNames.length; i++)
        {
        	if (Pattern.matches(".*_Story", fileNames[i]))
        	{
        		fileNamesFiltered.add(fileNames[i].substring(0, fileNames[i].length()-6));
        	}
        }
        fileNames = fileNamesFiltered.toArray(new String[fileNamesFiltered.size()]);
        setListAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, fileNames));
    }
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {    
		String filename = ((TextView) v).getText().toString();		
		Intent i = new Intent(savedStories.this, displayStories.class);
		i.putExtra("FileName", filename);
		startActivity(i);   
		
    }
	
}
